package org.ithang.application.bcRecord.service;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.ithang.application.account.bean.Account;
import org.ithang.application.account.service.AccountService;
import org.ithang.application.bcRecord.bean.BcRecord;
import org.ithang.application.bcRecord.mapper.BcRecordMapper;
import org.ithang.tools.database.ModelDao;
import org.ithang.tools.lang.DateUtils;
import org.ithang.tools.model.Arith;
import org.ithang.tools.model.Pager;
import org.ithang.tools.redis.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 充币记录 服务
 * @author zyt
 *
 */
@Service
public class BcRecordService extends ModelDao<BcRecord>{

    @Autowired
    private BcRecordMapper bcRecordMapper;
    
    @Autowired
    private AccountService accountService;

    private Logger logger = Logger.getLogger(BcRecordService.class);
    
    public void add(BcRecord bcRecord){
        bcRecordMapper.add(bcRecord);
    }
    
    public BcRecord get(Long id){
        return bcRecordMapper.get(id);
    }
    
    public BcRecord getTXHash(String txhash){
        return bcRecordMapper.getTXHash(txhash);
    }
    
    public int delete(Long id){
        return bcRecordMapper.delete(id);
    }
    
    public int batchDelete(String[] ids){
    	return bcRecordMapper.batchDelete(ids);
    }
    
    public List<BcRecord> list(Long... ids){
    	return bcRecordMapper.list(ids);
    }
    
    public List<BcRecord> page(Pager<BcRecord> page){
    	return bcRecordMapper.page(page);
    }
    
    public List<BcRecord> query(Map<String,Object> conditions){
    	return bcRecordMapper.query(conditions);
    }
    
    public int update(BcRecord bcRecord){
    	return bcRecordMapper.update(bcRecord);
    }
    
    @Transactional
    public void rechargeSuccess(String txJsond,String value,int customer_id,String txhash){
    	if(customer_id<=0){
    		return;
    	}
    	BcRecord bc=new BcRecord();
    	bc.setCurrency_id(1);
    	bc.setCustomer_id(customer_id);
    	bc.setResult(1);
    	bc.setAmount(Double.valueOf(value));
    	bc.setUpdate_time(DateUtils.getSystime());
    	bc.setCreate_time(DateUtils.getSystime());
    	bc.setOpt(txJsond);
    	bc.setTxhash(txhash);
    	
    	Account account=accountService.getByCustomer(customer_id);
    	account.setAmount(Arith.add(account.getAmount(),Double.valueOf(value)));
    	accountService.update(account);
    	add(bc);
    	JedisUtils.set(txhash, "ok");
    }
    
    @Transactional
    public void rechargeFail(String txJsond,String value,int customer_id,String txhash){
    	BcRecord bc=new BcRecord();
    	bc.setCurrency_id(1);
    	bc.setCustomer_id(customer_id);
    	bc.setAmount(Double.valueOf(value));
    	bc.setResult(0);
    	bc.setUpdate_time(DateUtils.getSystime());
    	bc.setCreate_time(DateUtils.getSystime());
    	bc.setOpt(txJsond);
    	bc.setTxhash(txhash);
    	add(bc);
    }
    
    
}